<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;

class Role extends Base
{
    use SoftDeletes;
    //
    protected $guarded = [];

    /**
     * 角色节点
     * @return BelongsToMany
     */
    public function nodes(): BelongsToMany
    {
        return $this->belongsToMany(Nodes::class, 'role_nodes', 'role_id', 'node_id');
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'user_role');
    }

    public function index(Request $request): mixed
    {
        return $this->makeQuery($request)->paginate($request->input('limit', 10));
    }

    public function makeQuery(Request $request): mixed
    {
        $query = $this->select('*');
        if ($request->has('name')) {
            $query->where('name', 'like', '%' . $request->input('name') . '%');
        }
        if ($request->has('status')) {
            $query->where('status', $request->input('status'));
        }
        return $query;
    }

}
